<?php


namespace app\data\model;

use think\admin\Model;

/**
 * 贷款资金表模型
 * Class Loans
 * @package app\data\model
 */
class Loans extends Model
{
    //贷款额度
    const LOAN_QUOTA = [
        ['id' => 1, 'start' => 0, 'end' => 2000000, 'text' => '200万及以下'],
        ['id' => 2, 'start' => 2000000, 'end' => 3000000, 'text' => '200-300万'],
        ['id' => 3, 'start' => 3000000, 'end' => 4000000, 'text' => '300-400万'],
        ['id' => 4, 'start' => 4000000, 'end' => 5000000, 'text' => '400-500万'],
        ['id' => 5, 'start' => 5000000, 'end' => 10000000, 'text' => '500-1000万'],
        ['id' => 6, 'start' => 10000000, 'end' => 0, 'text' => '1000万以上'],
    ];

    const LOAN_QUOTA_MAP = [
        1 => ['start' => 0, 'end' => 2000000,],
        2 => ['start' => 2000000, 'end' => 3000000,],
        3 => ['start' => 3000000, 'end' => 4000000,],
        4 => ['start' => 4000000, 'end' => 5000000,],
        5 => ['start' => 5000000, 'end' => 10000000,],
        6 => ['start' => 10000000, 'end' => 0,],
    ];

    //贷款期限
    const LOAN_TERM     = [
        ['id' => 1, 'start' => 0, 'end' => 3, 'text' => '3个月以内'],
        ['id' => 2, 'start' => 3, 'end' => 6, 'text' => '3-6个月'],
        ['id' => 3, 'start' => 6, 'end' => 12, 'text' => '6-12个月'],
        ['id' => 4, 'start' => 12, 'end' => 36, 'text' => '12-36个月'],
        ['id' => 5, 'start' => 36, 'end' => 0, 'text' => '36个月以上'],
    ];
    const LOAN_TERM_MAP = [
        1 => ['start' => 0, 'end' => 3,],
        2 => ['start' => 3, 'end' => 6,],
        3 => ['start' => 6, 'end' => 12,],
        4 => ['start' => 12, 'end' => 36,],
        5 => ['start' => 36, 'end' => 0,],
    ];

    public static function getLoanQuotaInfo($id): array
    {
        return self::LOAN_QUOTA_MAP[$id] ?? [];
    }

    public static function getLoanTermInfo($id): array
    {
        return self::LOAN_TERM_MAP[$id] ?? [];
    }

    public function institutionType()
    {
        return $this->hasOne(LoansInstitutionTypes::class, 'id', 'institution_type');
    }

    public function getServicesProcessList()
    {
        return LoansServiceProcess::query()->field(['id', 'cover_img', 'process_name', 'remark'])
            ->where('status', '=', 1)
            ->where('deleted', '=', 0)
            ->whereIn('id', explode(',', $this->services_process ?? ''))->select();
    }

    public function getMethodList()
    {
        return LoansMethod::query()->field(['id', 'method_name'])
            ->where('status', '=', 1)
            ->where('deleted', '=', 0)
            ->whereIn('id', explode(',', $this->method ?? ''))->select()->values();
    }
}
